<?php
class utils {
	/**
	 * Encrypts string with MD5 algorythm using salt
	 * 
	 * @param string $str string to encrypt
	 * @param string(9) $salt salt
	 * @return string
	 */
	public static function encrytp_str($str, $salt) {
		if (CRYPT_MD5 != 1) die("MD5 crypto algorythm is not supported!");

		$salt_len = strlen($salt);
		if ($salt_len < 1) die("Invalid salt!");

		if ($salt_len > 9) $salt = substr($salt, 0, 9);
		elseif ($salt_len < 9) $salt = substr(str_repeat($salt, ceil(9 / $salt_len)), 0, 9);

		$salt = '$1$'.$salt;
		return crypt($str, $salt);
	}
	/**
	 * Returns elapsed time from $time
	 *
	 * @param int $time
	 * @return string
	 */
	public static function elapsed_time($time) {
		$diff = time()-$time;
		$hrs = intval($diff / 3600);
		$secs = $diff % 3600;
		$mins = intval($secs /60);
		$secs = $secs % 60;

		return ($hrs ? $hrs.':' : ''). sprintf('%02u', $mins). ':' .sprintf('%02u', $secs);
	}

	/**
	 * Returns elapsed microtime from $timestart
	 *
	 * @param mixed $timestart value returned by microtime()
	 * @return string
	 */
	public static function elapsed_microtime($timestart) {
		$timeend = microtime();
		$diff = number_format(((substr($timeend,0,9)) + (substr($timeend,-10)) - (substr($timestart,0,9)) - (substr($timestart,-10))),4);
		return $diff." sec";
	}

	/**
	 * Strips variable slashes
	 *
	 * @param mixed $var
	 * @return mixed
	 */
	public static function strip_magic($var) {
		if(get_magic_quotes_gpc()) {
			if(is_array($var)) {
				$out = array();
				foreach($var as $key=>$val)
				$out[stripslashes($key)] = strip_magic($val);
			} else {
				$out = stripslashes($var);
			}
		} else {
			$out = $var;
		}
		return $out;
	}

	/**
	 * Redirects to URL $url
	 *
	 * @param string $url
	 */
	public static function redirect($url) {
		header('Location: '.$url);
		exit;
	}

	/**
	 * Converts seconds to seconds+minutes+...
	 *
	 * @param int $seconds
	 * @return array(years, months, weeks, days, hrs, mins, secs)
	 */
	public static function seconds_to_time($seconds) {
		$out = array(
		'years'		=> 0,
		'months'	=> 0,
		'weeks'		=> 0,
		'days'		=> 0,
		'hrs'		=> 0,
		'mins'		=> 0,
		'secs'		=> $seconds);

		while ($out['secs'] >= 60) {
			if ($out['secs'] >= (365*24*60*60)) {
				// more than 1 year
				$out['years'] = floor($out['secs'] / (365*24*60*60));
				$out['secs'] = $out['secs'] - ($out['years'] * (365*24*60*60));
			} elseif ($out['secs'] >= (86400*30)) {
				// more than 1 month
				$out['months'] = floor($out['secs'] / (86400*30));
				$out['secs'] = $out['secs'] - ($out['months'] * (86400*30));
			} elseif ($out['secs'] >= (86400*7)) {
				// more than 1 week
				$out['weeks'] = floor($out['secs'] / (86400*7));
				$seconds = $out['secs'] - ($out['weeks'] * (86400*7));
			} elseif ($out['secs'] >= 86400) {
				// more than 1 day
				$out['days'] = floor($out['secs'] / 86400);
				$out['secs'] = $out['secs'] - ($out['days'] * 86400);
			} elseif ($out['secs'] >= 3600) {
				$out['hrs'] = floor($out['secs'] / 3600);
				$out['secs'] = $out['secs'] - ($out['hrs'] * 3600);
			} elseif ($out['secs'] >= 60) {
				$out['mins'] = floor($out['secs'] / 60);
				$out['secs'] = $out['secs'] - ($out['mins'] * 60);
			}
		}
		if ($out['secs'] < 10) $out['secs'] = "0".$out['secs'];
		if ($out['mins'] < 10) $out['mins'] = "0".$out['mins'];

		return $out;
	}

	/**
	 * Generates random string
	 *
	 * @param int $length
	 * @param mixed $prefix
	 * @return string(32)
	 */
	public static function random_str($length = 32, $prefix = null) {
		$out = "";
		while(strlen($out) < $length) {
			$out .= uniqid(($prefix === false) ? "" : $prefix, true);
		}
		return substr($out, 0, $length);
	}

	/**
	 * Checks is the $class implements interface
	 * 
	 * @param mixed $class an object (class instance) or a string (class name)
	 * @param string $interface
	 * @return bool
	 */
	public static function is_implements($class, $interface) {
		$interfaces = class_implements($class, is_string($class));
		if ($interfaces === false) return false;
		return in_array($interface, $interfaces);
	}
	
	/**
	 * Converts any dimensional array to one dimension
	 *
	 * @param array $original_array
	 * @param array $result
	 */
	public static function array_to_one_dim_array($original_array, array &$result) {
		foreach ($original_array as $key => $value) {
			if (is_array($value)) self::array_to_one_dim_array($value, $result);
			else $result[] = $value;
		}
	}

	/**
	 * Includes all files in $path
	 * 
	 * @param string $path path to include folder
	 * @param bool $include_nested include nested folders files
	 */
	public static function include_directory($path, $include_nested = false) {
		if ($path[strlen($path) - 1] != '\\' && $path[strlen($path) - 1] != '/') $path .= '/';
		$h = opendir($path);
		if ($h === false) die("Error including directory: $path");
		while ($file = readdir($h)) {
			$include_path = $path.$file;
			if (is_dir($include_path) && $include_nested && !in_array($file, array(".", "..", ".svn"))) self::include_directory($include_path, $include_nested);
			if (is_file($include_path)) require_once($include_path);
		}
		closedir($h);
	}

	/**
	 * Makes string compatible to url string
	 *
	 * @param string $str
	 * @param int $maxlen
	 * @return string
	 */
	public static function get_linkable_string($str, $maxlen = null) {
		$out = preg_replace("|[^a-z0-9_]|", "_", strtolower($str));
		if (intval($maxlen) > 0) $out = substr($out, 0, $maxlen);

		return $out;
	}
	
	/**
	 * Extracts file extension from file name and removes it from file name
	 *
	 * @param string $file_name
	 * @return string
	 */
	public static function extract_file_ext(&$file_name) {
		$parts = explode(".", $file_name);
		if (count($parts) < 2) return "";
		
		$idx = count($parts) - 1;
		$ext = $parts[$idx];
		unset($parts[$idx]);
		
		$file_name = implode(".", $parts);
		
		return $ext;
	}
}
?>
